----------------------------------------------------------------
CREATE OR REPLACE FUNCTION public."widget_PatientCountNew"(
	active boolean DEFAULT NULL::boolean,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(*) from "Account" A
join "Patient" P on P."PatientId" = A."ReferenceId" and A."RoleId" = 4
where  A."Active"="active" and
case when "referenceId" is null then 1=1
 	else P."PatientId" in (
			with cts as (
			Select "PatientId" from "Appointment" where "ProviderId" = "referenceId"
			union all
			Select "PatientId" from "Admission" where "ProviderId" = "referenceId"
			)Select distinct cts."PatientId" from cts
		) 
	end;
End
$BODY$;
----------------------------------------------------------------
DROP FUNCTION public."widget_GetAppointmentCountByDate"(date, boolean);

CREATE OR REPLACE FUNCTION public."widget_GetAppointmentCountByDate"(
	"fromDate" date DEFAULT NULL::date,
	active boolean DEFAULT NULL::boolean,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select COUNT(*) from "Appointment" 
where "AppointmentDate"::date="fromDate" and "Active"="active"
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end;
end
$BODY$;
-------------------------------------
DROP FUNCTION public."widget_GetAdmissionCountByDate"(date, boolean);

CREATE OR REPLACE FUNCTION public."widget_GetAdmissionCountByDate"(
	"fromDate" date DEFAULT NULL::date,
	active boolean DEFAULT NULL::boolean,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select COUNT(*) from "Admission" 
where "AdmissionDate"::date="fromDate" and "Active"="active"
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end;
end
$BODY$;
----------------------------------------
CREATE OR REPLACE FUNCTION public."widget_New_Appointments_CountNew"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(*) from "Appointment" 
where "Active" is true and "FollowUpForAppointmentId" is null and "AppointmentDate"::date="fromDate"
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end;
end
$BODY$;
---------------------------------------
CREATE OR REPLACE FUNCTION public."widget_FollowUp_Appointments_CountNew"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(*) from "Appointment" 
where "Active" is true and "AppointmentDate"::date="fromDate" and 
	("FollowUpForAppointmentId" is not null or "FollowUpForAdmissionId" is not null)
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end;
end
$BODY$;
------------------------------------
DROP FUNCTION public."widget_GetDischargeCountByDate"(date);

CREATE OR REPLACE FUNCTION public."widget_GetDischargeCountByDate"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select COUNT(*)
from "Discharge" D
join "Admission" Adm on  Adm."AdmissionId"=D."AdmissionId"
where "DischargeDate"::date="fromDate"
and case when "referenceId" is null then 1=1 else Adm."ProviderId" = "referenceId" end;

end
$BODY$;
--------------------------------------------------
DROP FUNCTION public."widget_GetBookedBedCount"();

CREATE OR REPLACE FUNCTION public."widget_GetBookedBedCount"(
	"referenceId" integer DEFAULT NULL::integer
	)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(Distinct(B."BedId"))
from "Bed" B
 join "Admission" Adm on Adm."BedId"=B."BedId"
where B."BedStatusId" =2
and case when "referenceId" is null then 1=1 else Adm."ProviderId"= "referenceId" end;

end
$BODY$;
-------------------------------------------
DROP FUNCTION public."widget_GetBookedLabCountByDate"(date);

CREATE OR REPLACE FUNCTION public."widget_GetBookedLabCountByDate"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select COUNT(*) from "LabBookingHeader" 
where "BookingDate"="fromDate" and "Active" is true
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end;

end
$BODY$;
-------------------------
DROP FUNCTION public."widget_Labs_InPatients_Count"(date);

CREATE OR REPLACE FUNCTION public."widget_Labs_InPatients_Count"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(DISTINCT lbh."PatientId") 
from "LabBookingHeader" lbh
join "Admission" ad on ad."AdmissionId" = lbh."AdmissionId"
where lbh."Active" is true and lbh."BookingDate"::date="fromDate"::date
and case when "referenceId" is null then 1=1 else lbh."ProviderId"= "referenceId" end;

end
$BODY$;
---------------------------------------
DROP FUNCTION public."widget_Labs_OutPatients_Count"(date);

CREATE OR REPLACE FUNCTION public."widget_Labs_OutPatients_Count"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select count(distinct "PatientId") from "LabBookingHeader" 
where "AdmissionId" is null  and       --          "AppointmentId" is null and 
"Active" is true and "BookingDate"::date="fromDate"::date
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end;

end
$BODY$;
--------------------------------------------
DROP FUNCTION public."widget_Pharmacy_InPatients_Count"(date);

CREATE OR REPLACE FUNCTION public."widget_Pharmacy_InPatients_Count"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(distinct "PatientId")
from "PharmacyIndentHeader" PIH 
join "Admission" AD on AD."AdmissionId"=PIH."AdmissionId" 
where PIH."Status"='A' and PIH."ModifiedDate"::date="fromDate"
and case when "referenceId" is null then 1=1 else AD."ProviderId"= "referenceId" end;

end
$BODY$;

-----------------------------------------
DROP FUNCTION public."widget_Pharmacy_OutPatients_Count"(date);

CREATE OR REPLACE FUNCTION public."widget_Pharmacy_OutPatients_Count"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(distinct PSH."PatientId")
from "PharmacySaleHeader" PSH
where "SaleDate"::date="fromDate"
and case when "referenceId" is null then 1=1
	else PSH."PatientId" in(
	   select "PatientId" from "Appointment"  where "ProviderId"="referenceId"
	)end;

end
$BODY$;
----------------------------------------


